﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Collections.ObjectModel;
using Medianamik.Core;
using System.Web;

namespace Medianamik.UI
{
    /// <summary>
    /// Classe représentant un "Module" dans Médianamik
    /// </summary>
    public class MedianamikModule : SystemNode<MedianamikModule>, ISystemNode
    {
        #region .ctors

        public MedianamikModule()
            : base()
        {

        }

        #endregion

        #region Properties

        public override string DisplayName
        {
            get
            {
                return (string)HttpContext.GetGlobalResourceObject("SiteMap", Alias);
            }
        }


        #region ISystemNode Members

        public override Guid SystemNodeTypeID
        {
            get { return MedianamikTypes.MedianamikModule; }
        }

        #endregion

        #region Language Neutral Properties

		//public int DisplayOrder
		//{
		//    get
		//    {
		//        return DefaultInstance.Properties["DisplayOrder"].GetValue<int>();
		//    }
		//    set
		//    {
		//        DefaultInstance.Properties["DisplayOrder"].SetValue(value);
		//    }
		//}

        public string Alias
        {
            get
            {
                return DefaultInstance.Properties["Alias"].GetValue<string>();
            }
            set
            {
                DefaultInstance.Properties["Alias"].SetValue(value);
            }
        }

        public string Class
        {
            get
            {
                return DefaultInstance.Properties["Class"].GetValue<string>();
            }
			set
			{
				DefaultInstance.Properties["Class"].SetValue(value);
			}
        }

        #endregion

        

        private Collection<MedianamikPage> _pages;
        public Collection<MedianamikPage> Pages
        {
            get
            {
                if (_pages == null)
                {
                    _pages =  new Collection<MedianamikPage>(NodeManager.GetChildNodes<MedianamikPage>(ID , MedianamikTypes.MedianamikPage, false, 1));
                }

                return _pages;
            }
        }

		
		public Guid? SearchResultPageId
		{
			get
			{
				return DefaultInstance.Properties["SearchResultPage"].GetValue<Guid?>();
			}
			set
			{
				DefaultInstance.Properties["SearchResultPage"].SetValue(value);
			}
		}

        #endregion

        #region Methods

        public string GetName(string pCultureName)
        {
            var inst = Instances.Where(i => i.CultureName.Equals(pCultureName, StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();
            if (inst == null) { return null; }

            return inst.Properties[NamePropertyId].GetValue<string>();
        }
        public void SetName(string pValue, string pCultureName)
        {
            var inst = Instances.Where(i => i.CultureName.Equals(pCultureName, StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();
            if (inst == null) { inst = AddNodeInstance(pCultureName); }

            inst.Properties[NamePropertyId].SetValue(pValue);
        }

        #endregion
    }
}
